package com.zbkj.front.controller;

import com.zbkj.dal.common.exception.CrmebException;
import com.zbkj.dal.entity.order.DuesOrder;
import com.zbkj.dal.entity.user.User;
import com.zbkj.dal.pojo.request.PageParamRequest;
import com.zbkj.dal.base.result.CommonResult;
import com.zbkj.service.model.vo.DuesOrderListVO;
import com.zbkj.service.service.DuesOrderService;
import com.zbkj.service.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 会费接口
 */
@Slf4j
@RestController
@RequestMapping("api/front/dues")
@Api(tags = "充值订单控制器")
public class DuesController {

    @Autowired
    private UserService userService;
    @Autowired
    private DuesOrderService duesOrderService;

    @ApiOperation(value = "会费充值订单详情")
    @RequestMapping(value = "/detail/{orderNo}", method = RequestMethod.POST)
    public CommonResult<DuesOrder> detail(@PathVariable("orderNo") String orderNo) {
        DuesOrder duesOrder = duesOrderService.getByOrderNo(orderNo);
        if (duesOrder == null) {
            throw new CrmebException("订单不存在");
        }
        User userInfo = userService.getInfo();
        if (!ObjectUtils.nullSafeEquals(userInfo.getId(), duesOrder.getUid())) {
            CommonResult.failed("您无权查看该订单");
        }
        return CommonResult.success(duesOrder);
    }

    @ApiOperation(value = "我的会费充值订单列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<List<DuesOrderListVO>> listMyDuesOrder(Integer status, PageParamRequest pageRequest) {
        return CommonResult.success(duesOrderService.listMyDuesOrder(status, pageRequest));
    }

    /**
     * 删除我的订单
     * @param orderNo
     * @return
     */
    @ApiOperation(value = "删除我的订单")
    @RequestMapping(value = "/delete/{orderNo}", method = RequestMethod.POST)
    public CommonResult<Boolean> deleteMyOrder(@PathVariable("orderNo") String orderNo) {
        return CommonResult.success(duesOrderService.deleteMyOrder(orderNo));
    }
}
